package com.meirengu.webview.controller;

import com.alibaba.fastjson.JSONObject;
import com.meirengu.common.StatusCode;
import com.meirengu.model.Result;
import com.meirengu.utils.HttpUtil;
import com.meirengu.utils.MD5Util;
import com.meirengu.webview.model.PushTest;
import com.meirengu.webview.model.User;
import com.meirengu.webview.model.UserInfo;
import com.meirengu.webview.service.KwOperationLogService;
import com.meirengu.webview.service.PushTestService;
import com.meirengu.webview.service.UserInfoService;
import com.meirengu.webview.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("pushTest")
public class PushTestController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private KwOperationLogService kwOperationLogService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private PushTestService pushTestService;

    @ResponseBody
    @RequestMapping("/handTest")
    public Result handTest(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                           @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd) throws Exception {
        if (StringUtils.isBlank(createTimeBegin) || StringUtils.isBlank(createTimeEnd)) {
            return setResult(StatusCode.MISSING_ARGUMENT, null, StatusCode.codeMsgMap.get(StatusCode.MISSING_ARGUMENT));
        }
        //查出符合条件的用户
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            map.put("createTimeEnd", createTimeEnd);
        }
        List<Map<String, Object>> userList = userService.getList(map);
        kwOperationLogService.addOperationLog("1", " 推送渠道", "handTest",
                "发送测试数据", createTimeBegin, createTimeEnd, "kw_user", userList.size(),
                null, null, null);
        if (userList != null && userList.size() > 0) {
            for (Map<String, Object> userMap : userList) {
                String id = userMap.get("id").toString();
                PushTest pushTest = new PushTest();
                if (userMap.get("name") != null) {
                    UserInfo userInfo = userInfoService.getByUserId(id);
                    if (userInfo != null && StringUtils.isNotBlank(userInfo.getId())) {
                        User user = new User();
                        user.setId(id);
                        user.setName(userMap.get("name").toString());
                        user.setMobile(userMap.get("mobile").toString());
                        user.setSex(Integer.parseInt(userMap.get("sex").toString()));
                        String url = "http://39.106.210.85/product/receive";
                        String name = user.getName();
                        String channelNo = "dp_la_001";
                        String screct = "diwlsdfe";
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String applyTime = sdf1.format(userInfo.getCreateDate());
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("channelNo", channelNo);
                        jsonObject.put("name", name);
                        jsonObject.put("sex", user.getSex());
                        jsonObject.put("idCard", userInfo.getIdCard());
                        jsonObject.put("areaCode", userInfo.getAreaCode());
                        jsonObject.put("areaName", userInfo.getAreaName());
                        jsonObject.put("birthday", sdf.format(userInfo.getBirthday()));
                        jsonObject.put("expectAmount", userInfo.getExpectAmount());
                        jsonObject.put("policy", userInfo.getPolicy());
                        jsonObject.put("credit", userInfo.getCredit());
                        jsonObject.put("house", userInfo.getHouse());
                        jsonObject.put("car", userInfo.getCar());
                        jsonObject.put("fund", userInfo.getFund());
                        jsonObject.put("socialSecurity", userInfo.getSocialSecurity());
                        jsonObject.put("wxloan", userInfo.getWxloan());
                        jsonObject.put("occupation", userInfo.getOccupation());
                        jsonObject.put("workDuration", userInfo.getWorkDuration());
                        jsonObject.put("salaryMethod", userInfo.getSalaryMethod());
                        jsonObject.put("salary", userInfo.getSalary());
                        jsonObject.put("applyTime", applyTime);
                        jsonObject.put("ip", userInfo.getIp());
                        jsonObject.put("mobileRegistration", userInfo.getMobileRegistration());
                        String signCurrent = MD5Util.MD5(channelNo + name + screct + applyTime, "UTF-8");
                        jsonObject.put("sign", signCurrent);
                        HttpUtil.HttpResult hr = null;
                        try {
                            hr = HttpUtil.doPost(url, jsonObject.toString());
                            String content = hr.getContent();
                            JSONObject returnMsg = JSONObject.parseObject(content);
                            String rCode = returnMsg.getString("code");
                            String rMessage = returnMsg.getString("msg");
                            if (rCode.equals("200")) {
                                pushTest.setMobile(user.getMobile());
                                pushTest.setPushId((Integer) returnMsg.get("data"));
                                pushTestService.insert(pushTest);
                            }
                            System.out.println(rCode);
                            System.out.println(rMessage);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }
        return super.setResult(StatusCode.OK, "", StatusCode.codeMsgMap.get(StatusCode.OK));
    }


    @ResponseBody
    @RequestMapping("/getMobile")
    public Result getMobile(@RequestParam(value = "id") Integer pushId) {
        PushTest pushTest = pushTestService.detail(pushId);
        if (StringUtils.isNotBlank(pushTest.getMobile())) {
            return super.setResult(StatusCode.OK, pushTest.getMobile(), StatusCode.codeMsgMap.get(StatusCode.OK));
        }
        return super.setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
    }
}
